﻿#if UNITY_EDITOR

using UnityEditor;
using UnityEngine;

namespace AI.Graph.Editor {


    [CustomEditor (typeof (SubCondition), true)]
    public class SubConditionView : CollapsibleNodeView<SubCondition> {

        protected sealed override Vector2 OnDrawNode (bool drawSimple) {
            if (drawSimple) {
                Rect inputRect = DrawInput (0);

                Rect topRect = DrawNodeHeader (inputRect.yMax - 4);
                EditorGUI.DrawSolidTexture (topRect, Styles.outlineColor.color, new BorderWidth (0, 0, 0, 1));

                Rect spacingRect = DrawSpaceArea (topRect.yMax, 20);

                Rect outputRect = DrawOutput (spacingRect.yMax + Styles.footerHeight - 4);

                Rect footerRect = DrawNodeFooter (spacingRect.yMax);

                return new Vector2 (180, outputRect.yMax);
            }
            else {
                Rect inputRect = DrawInput (0);

                Rect topRect = DrawNodeHeaderWithToggle (inputRect.yMax - 4, ref node.isCollapsed);
                EditorGUI.DrawSolidTexture (topRect, Styles.outlineColor.color, new BorderWidth (0, 0, 0, 1));

                Rect spacingRect = DrawSpaceArea (topRect.yMax, 20);

                Rect collapseRect = DrawNodeCollapseArea (spacingRect.yMax);

                Rect outputRect = DrawOutput (collapseRect.yMax + Styles.footerHeight - 4);

                Rect footerRect = DrawNodeFooter (collapseRect.yMax);

                return new Vector2 (300, outputRect.yMax);
            }
        }


        /// <summary>
        /// Draws the input area.
        /// </summary>
        /// <param name="y">The start point.</param>
        /// <returns>Returns the size of the draw area.</returns>
        Rect DrawInput (float y) {
            Rect r = new Rect (node.size.x / 2 - node.size.x / 2, y, node.size.x, 45);

            EditorGUI.DrawSolidTexture (r, Styles.background300.color, new BorderRadius (4, 4, 0, 0));

            Vector2 portPosition = new Vector2 (node.position.x + r.center.x, node.position.y + 8);

            DrawConnectionPort (node.input, portPosition, Alignment.MiddleTop, LabelPosition.Bottom);

            EditorGUI.DrawSolidTexture (r, Styles.outlineColor.color, new BorderWidth (Styles.outlineWidth, Styles.outlineWidth, Styles.outlineWidth, 0), new BorderRadius (4, 4, 0, 0));

            return r;
        }


        /// <summary>
        /// Draws the output area.
        /// </summary>
        /// <param name="y">The start point.</param>
        /// <returns>Returns the size of the draw area.</returns>
        Rect DrawOutput (float y) {
            Rect r = new Rect (0, y, node.size.x, 45);

            EditorGUI.DrawSolidTexture (r, Styles.background300.color, new BorderRadius (0, 0, 4, 4));

            float padding = node.size.x / 3;
            float paddingBottom = 8;
            float portY = node.position.y + y + r.height - paddingBottom;

            Vector2 outputTruePosition = new Vector2 (node.position.x + padding, portY);
            Vector2 outputFalsePosition = new Vector2 (node.position.x + node.size.x - padding, portY);

            DrawConnectionPort (node.outputTrue, outputTruePosition, Alignment.MiddleBottom, LabelPosition.Top);
            DrawConnectionPort (node.outputFalse, outputFalsePosition, Alignment.MiddleBottom, LabelPosition.Top);

            EditorGUI.DrawSolidTexture (r, Styles.outlineColor.color, new BorderWidth (Styles.outlineWidth, Styles.outlineWidth, 0, Styles.outlineWidth), new BorderRadius (0, 0, 4, 4));

            return r;
        }


        internal override EdgePoint GetEdgePointForPort (ConnectionPort port) {
            if (port == node.input) {
                return new EdgePoint (node.input.rect.center, Vector2.up, colors.primaryColor2, colors.primaryColor1);
            }

            if (port == node.outputTrue) {
                return new EdgePoint (node.outputTrue.rect.center, Vector2.down, colors.primaryColor2, colors.primaryColor1);
            }

            if (port == node.outputFalse) {
                return new EdgePoint (node.outputFalse.rect.center, Vector2.down, colors.primaryColor2, colors.primaryColor1);
            }

            return base.GetEdgePointForPort (port);
        }

    }


}
#endif